'''
An example showing nearest point queries,
primitive volume sampling, oriented bounding boxes,
and using PointCloud objects for visualization
'''
import trimesh
import numpy as np
# load a large- ish PLY model with colors
mesh = trimesh.load('../models/cycloidal.ply')
# we can sample the volume of Box primitives
points = mesh.bounding_box_oriented.sample_volume(count=10)
# find the closest point on the mesh to each random point
(closest_points,
distances,
triangle_id) = mesh.nearest.on_surface(points)
print('Distance from point to surface of mesh:\n{}'.format(distances))
Distance from point to surface of mesh: [0.01267208 0.16778648 0.11539934 0.02974818 0.01285941 0.17135332 0.08874726 0.22368162 0.05983924 0.1831681 ]
# create a PointCloud object out of each (n,3) list of points
cloud_original = trimesh.points.PointCloud(points)
cloud_close = trimesh.points.PointCloud(closest_points)
# create a unique color for each point
cloud_colors = np.array([trimesh.visual.random_color() for i in points])
# set the colors on the random point and its nearest point to be the same
cloud_original.vertices_color = cloud_colors
cloud_close.vertices_color = cloud_colors
# create a scene containing the mesh and two sets of points
scene = trimesh.Scene([mesh,
cloud_original,
cloud_close])
# show the scene wusing
scene.show()
/usr/local/lib/python3.9/site-packages/IPython/core/display.py:414: UserWarning: Consider using IPython.display.IFrame instead
warnings.warn("Consider using IPython.display.IFrame instead")